home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1924 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  53 lines

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What should be returned?
  5. Date: Wed, 17 Jan 1996 21:30:31 GMT
  6. Organization: Netcom
  7. Message-ID: <30fd5c1a.5495936@nntp.ix.netcom.com>
  8. References: <4dj8pv$cjd@eng_ser1.erg.cuhk.hk>
  9. NNTP-Posting-Host: ix-dc8-02.ix.netcom.com
  10. X-NETCOM-Date: Wed Jan 17  1:30:29 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. phsung@cs.cuhk.hk (the CAReLess boy) wrote:
  14.  
  15. |>Hi, all,
  16. |>
  17. |>    It's said that the function main must return an integer value
  18. |>but I just don't know what value should be returned.  Also, if I
  19. quit
  20. |>main by exit(), what's the use of the return value?
  21. |>
  22. |>    Any help?
  23.  
  24. If you always use exit() from main, you need not have a return
  25. statement.  For example, the following is legal:
  26.  
  27.     #include <stdlib.h>
  28.     int main(void)
  29.     {
  30.       exit(0);
  31.     }
  32.  
  33. In fact, it's even legal to have neither a return or exit():
  34.  
  35.     int main(void)
  36.     {
  37.     }
  38.  
  39. This will return an undefined value to the operating environment, but
  40. does not result in undefined behavior and will not do damage.  I
  41. strongly recommend against this practice and many compilers will issue
  42. a warning message.
  43.  
  44. What is not legal is to define main as returning a type other than
  45. int.  The above would be illegal if main were defined as void
  46. main(void).  Defining main as returing anything but int results in
  47. undefined behavior; the standard allows the compiler to do anything
  48. (format your disk, send email to your boss complaining about your
  49. incompetence, ...).
  50.  
  51.  
  52. Michael M Rubenstein
  53.